<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('Request-Service详细信息')" />
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" id="add" onclick="$.operate.addTab()" shiro:hasPermission="system:request:add">
                <i class="fa fa-plus"></i> 添加
            </a>
            <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="system:request:export">
                <i class="fa fa-download"></i> 导出
            </a>

            <a class="btn btn-success" id="flow" onclick="" shiro:hasPermission="system:analyse:edit">
                <i class="fa fa-share"></i> 流转
            </a>

            <a class="btn btn-success" id="approve" onclick="submit()" shiro:hasPermission="system:analyse:edit">
                <i class="fa fa-check"></i> 提交审核
            </a>
        </div>

        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table" data-auto-refresh="true" data-auto-refresh-interval="10"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: bootstrap-table-auto-refresh-js" />
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('system:request:edit')}]];
    var removeFlag = [[${@permission.hasPermi('system:request:remove')}]];
    var analyseFlag = [[${@permission.hasPermi('system:analyse')}]];
    var serviceStatusDatas = [[${@dict.getType('sys_service_status')}]];
    var expStatusDatas = [[${@dict.getType('auto_exp_status')}]];
    var requestId = [[${requestId}]];
    var requestStatus = [[${requestStatus}]];
    var prefix = ctx + "system/analyse";
    var downloadUrl = prefix + "/download";
    var downloadUrl2 = prefix + "/download2";

    $(function() {
        if(requestStatus != 3 && requestStatus != 10){
            $('#add').attr("disabled","disabled")
        }
        var options = {
            url: prefix + "/detail/list",
            exportUrl: ctx + "system/analyse/export",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove/{id}",
            modalName: "Service",
            pagination: false,
            showSearch: false,
            onLoadSuccess: function (data){
                onLoadSuccess(data);
            },
            queryParams : {
                requestId: requestId
            },
            columns:[
                {
                    field: 'serviceId',
                    title: '主键',
                    visible: false
                },
                {
                    field: 'expcode',
                    title: 'expcode',
                    visible: false
                },
                {
                    field: 'serviceName',
                    title: '服务名'
                },
                {
                    field: 'sampleName',
                    title: '样品名'
                },
                {
                    field: 'serviceType.svtypeName',
                    title: '服务类型'
                },
                {
                    field: 'process',
                    title: '进程',
                    // visible: false,
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(expStatusDatas, value);
                    }
                },
                {
                    field: 'serviceStatus',
                    title: '状态',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(serviceStatusDatas, value);
                    }
                },
                {
                    field: 'template.templateName',
                    title: '模板'
                },
                {
                    field: 'expParams',
                    title: '参数',
                    visible: false,
                    formatter: function(value, row, index) {
                        if(row.expParams == null){
                            return '<a href="javascript:void(0)"></a>';
                        }
                        // return '<a href="'+downloadUrl+'?id='+row.nmrFile.fileId+'"><i class="fa fa-search"></i></a>';
                        return '<a href="javascript:void(0)"><i class="fa fa-search"></i></a>';
                    }
                },
                {
                    field: 'instrument.instName',
                    title: '仪器名'
                },
                {
                    field: 'instrument.type',
                    title: '仪器型号',
                    visible: false
                },
                {
                    field: 'priority',
                    title: '优先',
                    formatter: function (value, row, index) {
                        if (value == '0'){
                            return 'NO';
                        }else {
                            return 'YES';
                        }
                    }
                },
                {
                    field: 'userVerifier.userName',
                    title: '服务验证员'
                },
                {
                    field: 'verifyResult',
                    title: '验证意见',
                    visible: false
                },
                {
                    field: 'startTime',
                    title: '开始时间',
                    visible: false
                },
                {
                    field: 'endTime',
                    title: '结束时间',
                    visible: false
                },
                {
                    field: 'nmrFile.fileName',
                    title: '结果',
                    formatter:function (value, row, index) {
                        if(row.serviceStatus == 6){
                            return '<a href="'+downloadUrl+'?id='+row.nmrFile.fileId+'"><i class="fa fa-download"></i> '+value+'</a>';
                        }else {
                            return '';
                        }
                    }
                },
                {
                  title: '报告',
                  formatter:function (value, row, index) {
                      if(row.reportfileIds == null || row.reportfileIds.length == 0){
                          return '-';
                      }else if(row.reportfileIds.indexOf(",") == -1){
                          return '<a href="'+downloadUrl2+'?id='+row.reportFiles[0].fileId+'"><i class="fa fa-download"></i> '+row.reportFiles[0].fileName+'</a>';
                      }else {
                          return '<a href="javascript:void(0)" onclick="reports('+row.serviceId+')"><i class="fa fa-search"></i>';
                      }
                      return '-';
                  }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        var edit = '<a class="btn btn-success btn-xs ' + analyseFlag + '" href="javascript:void(0)" onclick="editfn(' + row.serviceStatus + ','+row.serviceId+')"><i class="fa fa-edit"></i>编辑</a> ';
                        var reSubmit = '<a class="btn btn-success btn-xs ' + analyseFlag + '" href="javascript:void(0)" onclick="editfn(' + row.serviceStatus + ','+row.serviceId+')"><i class="fa fa-edit"></i>重新提交</a> ';
                        var end = '<a class="btn btn-primary btn-xs ' + analyseFlag + '" href="javascript:void(0)" onclick="edit2fn(' + row.serviceStatus + ','+row.serviceId+')"><i class="fa fa-hourglass-end"></i>结束</a> ';
                        var remove = '<a class="btn btn-danger btn-xs ' + analyseFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.serviceId + '\')"><i class="fa fa-remove"></i>移除</a> ';
                        var edit_false = '<a disabled="disabled" class="btn btn-success btn-xs ' + analyseFlag + '" href="javascript:void(0)" ><i class="fa fa-edit"></i>编辑</a> ';
                        var remove_false = '<a disabled="disabled" class="btn btn-danger btn-xs ' + analyseFlag + '" href="javascript:void(0)" ><i class="fa fa-edit"></i>移除</a> ';
                        var upload_false = '<a disabled="disabled" class="btn btn-success btn-xs ' + analyseFlag + '" href="javascript:void(0)" ><i class="fa fa-file-text-o"></i>生成报告</a> ';
                        var report = '<a class="btn btn-success btn-xs ' + analyseFlag + '" href="javascript:void(0)" onclick="report('+row.serviceId+',\'' +row.serviceName+'_'+row.sampleName+'\')"><i class="fa fa-file-text-o"></i> 生成报告</a> ';
                        var uploadReport = '<a class="btn btn-success btn-xs ' + analyseFlag + '" href="javascript:void(0)" onclick="uploadReport('+row.serviceId+')"><i class="fa fa-upload"></i> 上传报告</a> ';
                        if (row.serviceStatus == 1 || row.serviceStatus == 2 || row.serviceStatus == 8 ){
                            //not assigned、Completed、Reject
                            actions.push(edit);
                            actions.push(remove);
                        }else if(row.serviceStatus == 9){
                            //error
                            actions.push(reSubmit);
                            actions.push(end)
                        }else if(row.serviceStatus == 5){
                            //pending verification
                            actions.push(upload_false);
                        }else if(row.serviceStatus == 6){
                            //verified
                            actions.push(report);
                            actions.push(uploadReport);
                        }else {
                            //
                            actions.push(edit_false);
                            actions.push(remove_false);
                        }
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    //初始化
    function onLoadSuccess(data){
        //approve
        if(requestStatus == 12){  //verified
            $("#approve").show();
        }else {
            $("#approve").hide();
        }

        //flow
        if(requestStatus == 3){     //Open
            $("#flow").show();
        }else {
            $("#flow").hide();
        }
    }

    //修改
    function editfn(status, serviceId) {
        if(status == 1 || status == 2  || status == 8 || status == 9){
            $.operate.edit(serviceId);
        }else {
            $.modal.msgWarning("请检查服务状态")
        }
    }

    //提交审核
    function submit(){
        $.modal.confirm("确定要提交审核Request吗？", function (){
            $.operate.post(prefix +"/submit", {"requestId": requestId});
        })
    }

    //编辑报告
    function report(serviceId, name){
        var url = prefix+"/report?id="+serviceId;
        $.modal.openFull(name, url);
    }

    //上传报告
    function uploadReport(serviceId){
        $.modal.open('上传文件', prefix + "/upload?serviceId=" + serviceId);
    }

    //结束Request
    function edit2fn(status, serviceId) {
        console.log(status)
        if(status == 9){
            $.modal.confirm("确定要结束此Request吗？", function (){
                $.operate.post(ctx + "system/request/end", {"requestId": requestId, "requestStatus": 10} );
            })
        }
    }

</script>
</body>
</html>